#include "mainwindowimpl.h"

#define SIGN(a) (a>0)?1:0

MainWindowImpl::MainWindowImpl( QWidget * parent, Qt::WFlags f) 
	: QMainWindow(parent, f)
{
        setupUi(this);
	
        /*!	\fn Connection of signals         */
        QObject::connect(metScope, SIGNAL(clicked()), this, SLOT(setInits()));	// Labels
	QObject::connect(metCil, SIGNAL(clicked()), this, SLOT(setInits()));

        QObject::connect(paramQ1, SIGNAL(clicked()), this, SLOT(setParams()));
        QObject::connect(paramQ2, SIGNAL(clicked()), this, SLOT(setParams()));
        QObject::connect(paramQ3, SIGNAL(clicked()), this, SLOT(setParams()));
        QObject::connect(group_method, SIGNAL(clicked()), this, SLOT(setParams()));
        QObject::connect(but_printParam, SIGNAL(pressed()), this, SLOT(initCalc()));

        QObject::connect(waveTable, SIGNAL(doubleClicked(QModelIndex)), this, SLOT(showAlert(QModelIndex)));	//Alert
	QObject::connect(actionCopyRecord, SIGNAL(triggered()), this, SLOT(copyRecord()));	//Copy cells
	QObject::connect(actionCopyDatabase, SIGNAL(triggered()), this, SLOT(copyDatabase()));	//Copy Database
	QObject::connect(actionClear, SIGNAL(triggered()), this, SLOT(clearDatabase()));	//Clear Database
        QObject::connect(actionSave, SIGNAL(triggered()), this, SLOT(save()));	//Save components
        QObject::connect(actionAboutNefer, SIGNAL(triggered()), this, SLOT(about()));	//Save components
	
        /*!	\fn Init Database	*/
        QSqlQuery query;
        query.exec("DELETE from WAVE");
        Base[0]="id";
        Base[1]="realE"; Base[2]="imagE";	//AFR
        Base[3]="Eq1_re"; Base[4]="Eq1_im";	//R at scope&cyl - real&image
        Base[5]="Eq2_re"; Base[6]="Eq2_im";	//Teta at scope, Fi at cyl - real&image
        Base[7]="Eq3_re"; Base[8]="Eq3_im";	//Fi at scope, Z at cyl - real&image
        Base[9]="q1"; Base[10]="q2"; Base[11]="q3";	//coordinates of point
        Base[12]="rot_q1_re"; Base[13]="rot_q1_im"; Base[14]="rot_q2_re";	//rotor ingridients: real
        Base[15]="rot_q2_im"; Base[16]="rot_q3_re"; Base[17]="rot_q3_im";	//rotor ingridients: complex
        Base[18]="grad_q1_re"; Base[19]="grad_q1_im"; Base[20]="grad_q2_re";	//gradient ingridients: real
        Base[21]="grad_q2_im"; Base[22]="grad_q3_re"; Base[23]="grad_q3_im";	//rotor ingridients: complex
        Base[24]="j_q1_re"; Base[25]="j_q1_im"; Base[26]="j_q2_re";		//vector ingridients: real
        Base[27]="j_q2_im"; Base[28]="j_q3_re"; Base[29]="j_q3_im";	//rotor ingridients: complex*/
        Base[30]="flag_area"; Base[31]="flag_method";
        Base[32]="Amp"; Base[33]="Arg";
        QString queryText="id INTEGER PRIMARY KEY";
        for (int j=1;j<Base.count();j++) queryText += ", "+Base.value(j);
        query.exec("CREATE TABLE wave ("+queryText+")");
	
        /*!	\fn QTableView initialization	 */
	waveSqlModel = new QSqlTableModel(this);
	waveSqlModel->setTable("wave");
	waveSqlModel->select();
        waveTable->setModel(waveSqlModel);
	waveTable->resizeColumnsToContents();
	waveTable->addAction(actionCopyRecord);
	waveTable->addAction(actionCopyDatabase);
	waveTable->addAction(actionClear);
        waveTable->setColumnHidden(0,1);
        for (int j=12; j<32 ; j++) waveTable->setColumnHidden(j,1);
	waveSqlModel->select();
}

void MainWindowImpl::setInits()
{
        if(metScope->isChecked())
        {
            label_q1->setText("R/L");
            label_q2->setText("Teta");
            label_q3->setText("Fi");
            boxQ1->setValue(10);
            boxQ1->setMinimum(1);
            boxQ1->setMaximum(1000);
            boxQ2->setValue(15);
            boxQ3->setMinimum(-90);
            boxQ2->setMaximum(90);
            boxQ3->setValue(30);
            boxQ3->setMinimum(-360);
            boxQ3->setMaximum(360);
            boxQ3->setSuffix("");
            paramQ2->setChecked(1);
        }
        if(metCil->isChecked())
        {
            label_q1->setText("R/b");
            label_q2->setText("Fi");
            label_q3->setText("Z/L");
            boxQ1->setValue(1);
            boxQ1->setMinimum(0);
            boxQ1->setMaximum(20);
            boxQ2->setValue(0);
            boxQ2->setMinimum(-360);
            boxQ2->setMaximum(360);
            boxQ3->setValue(3);
            boxQ3->setMinimum(0);
            boxQ3->setMaximum(1000);
            boxQ3->setSuffix(" mm");
            paramQ1->setChecked(1);
        }
        this->setParams();
}

void MainWindowImpl::setParams()
{
        if(paramQ1->isChecked())
        {
            boxQ1->setDisabled(1); boxQ2->setEnabled(1); boxQ3->setEnabled(1);
            label_11->setText(label_q1->text()+"_0 =");
            label_16->setText(label_q1->text()+"_N =");
            boxParam0->setValue(0);
            boxParam0->setMinimum(0);
            boxParamN->setMaximum(100);
            boxParamN->setValue(2);
        }
        else
        {
            if(paramQ2->isChecked())
            {
                boxQ2->setDisabled(1); boxQ1->setEnabled(1); boxQ3->setEnabled(1);
                label_11->setText(label_q2->text()+"_0 =");
                label_16->setText(label_q2->text()+"_N =");
            }
            if(paramQ3->isChecked())
            {
                boxQ3->setDisabled(1); boxQ1->setEnabled(1); boxQ2->setEnabled(1);
                label_11->setText(label_q3->text()+"_0 =");
                label_16->setText(label_q3->text()+"_N =");
            }
            boxParam0->setMinimum(0);
            boxParam0->setValue(0);
            boxParamN->setValue(90);
            boxParamN->setMaximum(360);
        }
}

void MainWindowImpl::showAlert(QModelIndex)
{
    /// @todo Alert messages alerting
    QMessageBox::information(0, qApp->tr("Editing not Allowed"),	//Message title, first of all is iconSet
                             qApp->tr("Editing not Allowed.\n"
                                      "Please copy data to another application"),	//Message text
                             QMessageBox::Close);				//ButtonRole define
}
